package com.guigu.www.erp.dengtao.controller;

import com.guigu.www.erp.common.pojo.DFile;
import com.guigu.www.erp.common.pojo.MDesignProcedure;
import com.guigu.www.erp.common.pojo.MDesignProcedureDetails;
import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.dengtao.service.DTDFileService;
import com.guigu.www.erp.dengtao.service.MDesignProcedureDetailsService;
import com.guigu.www.erp.dengtao.service.MDesignProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/DFile")
public class DTDFileController {

    @Autowired
    private DTDFileService dFileService;
    @Autowired
    private MDesignProcedureService mDesignProcedureService;
    @Autowired
    private MDesignProcedureDetailsService mDesignProcedureDetailsService;

    @PostMapping("/like")
    public PageVo<DFile> findDFileLike(@RequestBody Map<String,String> map){
        return dFileService.pageDFile(map);
    }

    @PostMapping("/addgx")
    public Integer addgx(@RequestBody MDesignProcedure mDesignProcedure ) {

        Calendar now = Calendar.getInstance();
        String time = "400";
        //年
        time += now.get(Calendar.YEAR);
        //月
        String month = (now.get(Calendar.MONTH) + 1) + "";
        month = month.length() == 1 ? "0" + month : month;
        time += month;

        //日
        String day = now.get(Calendar.DAY_OF_MONTH) + "";
        day = day.length() == 1 ? "0" + day : day;
        time += day;

        //查询最大编号的方法
        String s = mDesignProcedureService.select();
        if (s == null) {
            time += "0001";
        } else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str = i + "";
            if (str.length() == 1) {
                str = "000" + str;
            } else if (str.length() == 2) {
                str = "00" + str;
            } else if (str.length() == 3) {
                str = "0" + str;
            }
            time += str;
        }

        mDesignProcedure.setDesignId(time);
        mDesignProcedure.setCheckTag("0");//审核字段
        mDesignProcedure.setChangeTag("0");//变更字段
        mDesignProcedure.setDesignModuleTag("0");//工序物料设计
        mDesignProcedure.setDesignModuleChangeTag("0");//工序物料变更

        mDesignProcedureService.addgx(mDesignProcedure);


        //修改档案设计字段改成以设计
        DFile dFile = new DFile();
        dFile.setProductId(mDesignProcedure.getProductId());

        dFileService.up(dFile);

        //往产品生产工序明细添加
        List<MDesignProcedureDetails> list = mDesignProcedure.getList();
        for (int j = 0; j < list.size(); j++) {
            String a = mDesignProcedureDetailsService.select();
            String time1 = "201";
            time1 += now.get(Calendar.YEAR);
//月
            String month1 = (now.get(Calendar.MONTH) + 1) + "";
            month1 = month1.length() == 1 ? "0" + month1 : month1;
            time1 += month1;

//日
            String day1 = now.get(Calendar.DAY_OF_MONTH) + "";
            day1 = day1.length() == 1 ? "0" + day1 : day1;
            time1 += day1;

            if (a == null) {
                time1 += "0001";
            } else {
                Integer ie = Integer.parseInt(a.substring(11));
                ie++;
                String str = ie + "";
                if (str.length() == 1) {
                    str = "000" + str;
                } else if (str.length() == 2) {
                    str = "00" + str;
                } else if (str.length() == 3) {
                    str = "0" + str;
                }
                time1 += str;
            }
            list.get(j).setProcedureId(time1);
            list.get(j).setParentId(mDesignProcedure.getId());

            mDesignProcedureDetailsService.add(list.get(j));

        }

        return 0;
    }

    @PostMapping("/shlike")
    public PageVo<MDesignProcedure> findMDesignProceduresh(@RequestBody Map<String,String> map, HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser.getRole().getRoleId()==4){
            return null;
        }
        return mDesignProcedureService.findshcx(map);
    }
    @PostMapping("/gxmzlike/{id}")
    public PageVo<MDesignProcedureDetails> gxmxAll(@PathVariable String id ){
        return mDesignProcedureDetailsService.FindAll(id);

    }
    @PostMapping("/cpgxsjdcx")
    public PageVo<MDesignProcedure> cpgxsjdcx(@RequestBody Map<String,String> map){
        return mDesignProcedureService.sjdcxsy(map);
    }

    @PutMapping("tongguo/{id}")
    public Integer tongguoxiugai(@PathVariable String id){
        mDesignProcedureService.xiugai(id,new Date().toLocaleString());
        return 0;
    }
    @PutMapping("butongguo/{id}")
    public Integer butongguoxiugai(@PathVariable String id){
        mDesignProcedureService.xiugaibutguo(id);
        return 0;
    }
    @PostMapping("shbtg")
    public PageVo<MDesignProcedure> ddshbtgbg(@RequestBody Map<String,String> map){
        return mDesignProcedureService.bgsele(map);
    }
    @PostMapping("cxgxmx/{id}")
    public List<MDesignProcedureDetails> cxgxmx(@PathVariable String id){
       return mDesignProcedureDetailsService.gxmx(id);
    }
    @PutMapping("gxmxxiugai")
    public int gxxiug(@RequestBody MDesignProcedure mDesignProcedure) {
        mDesignProcedure.setChangeTime(new Date().toLocaleString());
        mDesignProcedureService.zongxiugai(mDesignProcedure);
        List<MDesignProcedureDetails> list = mDesignProcedure.getList();
        for (int j = 0; j < list.size(); j++) {
            mDesignProcedureDetailsService.xiugai(list.get(j));
        }
        return 1;
    }

    @PostMapping("ckks/{id}")
    public MDesignProcedure ckks(@PathVariable String id){
        return  mDesignProcedureService.cxyg(id);

    }
    @PostMapping("jdcx/{id}")
    public DFile ckyx(@PathVariable String id){
        return dFileService.cxyx(id);
    }

    @PostMapping("ckk/{id}")
    public   List<MDesignProcedureDetails> cxx(@PathVariable String id){
        System.out.println(mDesignProcedureDetailsService.cx(id));
      return   mDesignProcedureDetailsService.cx(id) ;
    }
}
